package com.liuyuncen.controller;

import com.liuyuncen.entity.Feature;
import com.liuyuncen.entity.Person;
import com.liuyuncen.entity.Svo;
import com.liuyuncen.repository.PersonRepository;
import com.liuyuncen.repository.SvoRepository;
import org.neo4j.driver.Driver;
import org.neo4j.driver.Record;
import org.neo4j.driver.Result;
import org.neo4j.driver.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

import static org.neo4j.driver.Values.parameters;

/**
 * @belongsProject: SpringBoot-Neo4j
 * @belongsPackage: com.liuyuncen.controller
 * @author: Xiang想
 * @createTime: 2022-09-28  14:36
 * @description: TODO
 * @version: 1.0
 */
@RestController
@RequestMapping("/neo4j")
public class Neo4jController {


    @Autowired
    PersonRepository repository;

    @Autowired
    SvoRepository svoRepository;

    @Autowired
    Driver driver;


    @GetMapping("/findChildList")
    public List<Person> findChildList(Long id){
        System.out.println("id : "+id);
        List<Person> childList = repository.findChildList(id);
        for (Person person : childList) {
            System.out.println("person = " + person.toString());
        }
        return childList;
    }

    @GetMapping("/findSvoFeatureByCode")
    public List<Feature> findSvoFeatureByCode(String code){
        List<Feature> list = this.svoRepository.findFeatureByCode(code);
        return list;
    }

//    @GetMapping("/findSimilar")
//    public Object findSimilar(){
//        Object obj = this.svoRepository.findSimilar();
//        return obj;
//    }


    @GetMapping("/createPerson")
    public String createPerson(){
        Session session = driver.session();

        session.run("CREATE (n:Person {name:$name, age:$age})",
                parameters("name","妮妮同学","age","20"));
        session.run("CREATE (n:Person {name:$name, age:$age})",
                parameters("name","想想同学","age","24"));
        session.run("CREATE (n:Person {name:$name, age:$age})",
                parameters("name","叙同学","age","18"));


        Result result = session.run( "MATCH (n:Person) WHERE n.name = $name " +
                        "RETURN n.name as name, n.age as age",
                parameters( "name", "妮妮同学"));
        while (result.hasNext()) {
            Record record = result.next();
            System.out.println(record.get("name") +":"+ record.get("age"));
        }
        session.close();
        return "ok";
    }

}
